package com.apotik.legenda.dialog;

import javax.swing.JDialog;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.border.EmptyBorder;

import java.awt.GridLayout;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;

import com.apotik.legenda.dao.ProductDao;
import com.apotik.legenda.dao.PurchaseItemDao;
import com.apotik.legenda.frameinternal.PembelianInternalFrame;
import com.apotik.legenda.model.Product;
import com.apotik.legenda.model.Purchases;
import com.apotik.legenda.model.PurchasesItem;
import com.apotik.legenda.model.User;
import com.apotik.legenda.observer.GeneralObserver;
import com.apotik.legenda.util.AppUtil;
import com.apotik.legenda.util.CalendarUtil;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class AddEditPurchaseItemDialog extends JDialog  implements ActionListener, GeneralObserver, FocusListener, KeyListener {
	private static final long serialVersionUID = 1L;
	
	private Product productSelected;
	private Purchases purcahse;
	private GeneralObserver observer;
	private User user;
	private int selectedMonthIndex;
	//private int selectedDayIndex;
	
	private JTextField textFieldNamaBarang;
	private JTextField textFieldKodeBarang;
	private JTextField textFieldHargaSatuan;
	private JTextField textFieldIsiKemasan;
	private JTextField textFieldPpnPersen;
	private JTextField textFieldPpnAmount;
	private JTextField textFieldMarkupPersen;
	private JTextField textFieldMarkupAmount;
	private JTextField textFieldHargaJual;
	private JTextField textFieldHargaJuaEceran;

	private JButton btnCariBarang;
	private JButton btnSubmit;
	private JButton btnCancel;
	
	//@SuppressWarnings("rawtypes")
	//private JComboBox comboBoxTglExp; 
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxBlnExp;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxThnExp;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxSatPak;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBoxSatKemasan;
	private JTextField textFieldHargaBeli;
	private JTextField textFieldJumlahPak;
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public AddEditPurchaseItemDialog(String title, Purchases purcahse, GeneralObserver observer, User user) {
		this.purcahse = purcahse;
		this.observer = observer;
		this.user = user;
		
		
		setTitle(title);
		setSize(400, 500);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(0, 2, 0, 3));
		
		JLabel lblKodeBarang = new JLabel("Kode Barang");
		lblKodeBarang.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblKodeBarang);
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new BorderLayout(2, 0));
		
		btnCariBarang = new JButton("Cari");
		btnCariBarang.addActionListener(this);
		
		textFieldKodeBarang = new JTextField();
		panel_2.add(textFieldKodeBarang, BorderLayout.CENTER);
		textFieldKodeBarang.setEnabled(false);
		textFieldKodeBarang.setColumns(10);
		panel_2.add(btnCariBarang, BorderLayout.EAST);
		
		JLabel lblNamaBarang = new JLabel("Nama Product");
		panel_1.add(lblNamaBarang);
		lblNamaBarang.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		textFieldNamaBarang = new JTextField();
		panel_1.add(textFieldNamaBarang);
		textFieldNamaBarang.setEnabled(false);
		textFieldNamaBarang.setColumns(10);
		
		JLabel lblTanggalExpire = new JLabel("Tanggal Expire");
		lblTanggalExpire.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblTanggalExpire);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new BorderLayout(3, 0));
		
		/*comboBoxTglExp = new JComboBox();
		comboBoxTglExp.setModel(comboboxModelDays());
		comboBoxTglExp.setSelectedIndex(selectedDayIndex);
		panel_3.add(comboBoxTglExp, BorderLayout.WEST);*/
		
		comboBoxBlnExp = new JComboBox();
		comboBoxBlnExp.setModel(comboboxModelMonth());
		comboBoxBlnExp.setSelectedIndex(selectedMonthIndex);
		panel_3.add(comboBoxBlnExp, BorderLayout.CENTER);
		
		comboBoxThnExp = new JComboBox();
		comboBoxThnExp.setModel(comboboxModelYear());
		panel_3.add(comboBoxThnExp, BorderLayout.EAST);
		
		JLabel lblSatuanPak = new JLabel("Satuan Pak");
		lblSatuanPak.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblSatuanPak);
		
		comboBoxSatPak = new JComboBox();
		comboBoxSatPak.setEnabled(false);
		comboBoxSatPak.setModel(new DefaultComboBoxModel(new String[] {"BOX"}));
		panel_1.add(comboBoxSatPak);
		
		JLabel lblJumlahPak = new JLabel("Jumlah Pak");
		lblJumlahPak.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblJumlahPak);
		
		textFieldJumlahPak = new JTextField();
		panel_1.add(textFieldJumlahPak);
		textFieldJumlahPak.setColumns(10);
		
		JLabel lblHargaSatuan = new JLabel("Harga Satuan");
		lblHargaSatuan.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblHargaSatuan);
		
		textFieldHargaSatuan = new JTextField();
		textFieldHargaSatuan.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_1.add(textFieldHargaSatuan);
		textFieldHargaSatuan.setColumns(10);
		textFieldHargaSatuan.addFocusListener(this);
		textFieldHargaSatuan.addKeyListener(this);
		
		JLabel lblPpn = new JLabel("PPN");
		lblPpn.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblPpn);
		
		JPanel panel_4 = new JPanel();
		panel_1.add(panel_4);
		panel_4.setLayout(new BorderLayout(3, 0));
		
		textFieldPpnPersen = new JTextField();
		textFieldPpnPersen.setEnabled(false);
		textFieldPpnPersen.setHorizontalAlignment(SwingConstants.LEFT);
		textFieldPpnPersen.setText("10 %");
		panel_4.add(textFieldPpnPersen, BorderLayout.WEST);
		textFieldPpnPersen.setColumns(10);
		textFieldPpnPersen.addFocusListener(this);
		
		textFieldPpnAmount = new JTextField();
		textFieldPpnAmount.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldPpnAmount.setText("000.000");
		textFieldPpnAmount.setEnabled(false);
		panel_4.add(textFieldPpnAmount, BorderLayout.CENTER);
		textFieldPpnAmount.setColumns(10);
		
		JLabel lblHargaBeli = new JLabel("Harga Beli");
		lblHargaBeli.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblHargaBeli);
		
		textFieldHargaBeli = new JTextField();
		textFieldHargaBeli.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldHargaBeli.setEnabled(false);
		panel_1.add(textFieldHargaBeli);
		textFieldHargaBeli.setColumns(10);
		
		JLabel lblMarkUp = new JLabel("Mark Up");
		lblMarkUp.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblMarkUp);
		
		JPanel panel_5 = new JPanel();
		panel_1.add(panel_5);
		panel_5.setLayout(new BorderLayout(3, 0));
		
		textFieldMarkupPersen = new JTextField();
		textFieldMarkupPersen.setText("25 %");
		textFieldMarkupPersen.setHorizontalAlignment(SwingConstants.LEFT);
		panel_5.add(textFieldMarkupPersen, BorderLayout.WEST);
		textFieldMarkupPersen.setColumns(10);
		textFieldMarkupPersen.addFocusListener(this);
		
		textFieldMarkupAmount = new JTextField();
		textFieldMarkupAmount.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldMarkupAmount.setText("000.000");
		textFieldMarkupAmount.setEnabled(false);
		panel_5.add(textFieldMarkupAmount, BorderLayout.CENTER);
		textFieldMarkupAmount.setColumns(10);
		
		JLabel lblHargaJualPak = new JLabel("Harga Jual");
		lblHargaJualPak.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblHargaJualPak);
		
		textFieldHargaJual = new JTextField();
		textFieldHargaJual.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldHargaJual.setEnabled(false);
		panel_1.add(textFieldHargaJual);
		textFieldHargaJual.setColumns(10);
		
		JLabel lblSatuanKemasan = new JLabel("Satuan Kemasan");
		lblSatuanKemasan.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblSatuanKemasan);
		
		comboBoxSatKemasan = new JComboBox();
		comboBoxSatKemasan.setModel(new DefaultComboBoxModel(new String[] {"TAB", "PCS", "BTL", "SET", "CAP"}));
		panel_1.add(comboBoxSatKemasan);
		
		JLabel lblIsiKemasan = new JLabel("Isi Kemasan");
		lblIsiKemasan.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblIsiKemasan);
		
		textFieldIsiKemasan = new JTextField();
		panel_1.add(textFieldIsiKemasan);
		textFieldIsiKemasan.setColumns(10);
		textFieldIsiKemasan.addFocusListener(this);
		
		JLabel lblHargaJualEceran = new JLabel("Harga Jual Eceran");
		lblHargaJualEceran.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_1.add(lblHargaJualEceran);
		
		textFieldHargaJuaEceran = new JTextField();
		textFieldHargaJuaEceran.setHorizontalAlignment(SwingConstants.RIGHT);
		textFieldHargaJuaEceran.setEnabled(false);
		panel_1.add(textFieldHargaJuaEceran);
		textFieldHargaJuaEceran.setColumns(10);
		
		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new MatteBorder(1, 0, 0, 0, (Color) SystemColor.controlShadow));
		panel.add(panel_6, BorderLayout.SOUTH);
		panel_6.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_7 = new JPanel();
		panel_7.setBorder(new EmptyBorder(5, 5, 5, 0));
		panel_6.add(panel_7, BorderLayout.CENTER);
		panel_7.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_8 = new JPanel();
		panel_7.add(panel_8, BorderLayout.EAST);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(this);
		panel_8.add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(this);
		panel_8.add(btnCancel);
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel comboboxModelYear(){
		DefaultComboBoxModel model = new DefaultComboBoxModel<>();
		//model.addElement(" - Tahun -");
		
		Calendar cal = CalendarUtil.calendarNow();
		int yearNow = cal.get(Calendar.YEAR);
		
		model.addElement(Integer.toString(yearNow));
		model.addElement(Integer.toString(yearNow+1));
		model.addElement(Integer.toString(yearNow+2));
		model.addElement(Integer.toString(yearNow+3));
		model.addElement(Integer.toString(yearNow+4));
		
		return model;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel comboboxModelMonth(){
		DefaultComboBoxModel model = new DefaultComboBoxModel<>();
		//model.addElement(" - Bulan -");
		
		Calendar cal = CalendarUtil.calendarNow();
		int monthNow = cal.get(Calendar.MONTH);
		
		model.addElement("JANUARI");
		model.addElement("FEBRUARI");
		model.addElement("MARET");
		model.addElement("APRIL");
		model.addElement("MEI");
		model.addElement("JUNI");
		model.addElement("JULI");
		model.addElement("AGUSTUS");
		model.addElement("SEPTEMBER");
		model.addElement("OKTOBER");
		model.addElement("NOVEMBER");
		model.addElement("DESEMBER");
		
		selectedMonthIndex = monthNow;
		
		return model;
	}
	
	/*@SuppressWarnings({ "rawtypes", "unchecked" })
	public DefaultComboBoxModel comboboxModelDays(){
		DefaultComboBoxModel model = new DefaultComboBoxModel<>();
		//model.addElement(" - Tgl -");
		
		Calendar cal = CalendarUtil.calendarNow();
		int dayNow = cal.get(Calendar.DAY_OF_MONTH);
		
		for(int i=1;i<32;i++){
			model.addElement(Integer.toString(i));
		}
		
		selectedDayIndex = dayNow-1;
		
		return model;
	}*/
	
	private int removePersen(String param){
		int result = 0;
		
		String[] pisah = param.split(" %");
		
		if(pisah.length>0){
			String first = pisah[0];
			if(AppUtil.isNumber(first)){
				result = Integer.parseInt(pisah[0]);
			}
		}
		
		return result;
	}

	private void updateTextFieldPrice(){
		//try {
			
			String valHargaSatuanStr = textFieldHargaSatuan.getText();
			if(valHargaSatuanStr.isEmpty()){
				valHargaSatuanStr = "0";
			}
			String removeDotHargaSatuan = AppUtil.removeCharacters(valHargaSatuanStr, ".");
			BigDecimal valHargaSatuanBg = new BigDecimal(removeDotHargaSatuan);
			
			int ppnPersen = removePersen(textFieldPpnPersen.getText());
			BigDecimal ppnAmount = AppUtil.amountPersentasi(ppnPersen, valHargaSatuanBg, 0);
			textFieldPpnAmount.setText(AppUtil.formatStringIDR(ppnAmount.intValue()));
			
			BigDecimal hargaBeliBg = valHargaSatuanBg.add(ppnAmount);
			textFieldHargaBeli.setText(AppUtil.formatStringIDR(hargaBeliBg.intValue()));
			
			int markupPersen = removePersen(textFieldMarkupPersen.getText());
			BigDecimal markupAmount = AppUtil.amountPersentasi(markupPersen, hargaBeliBg, 0);
			textFieldMarkupAmount.setText(AppUtil.formatStringIDR(markupAmount.intValue()));
			
			BigDecimal hargaJualBg = hargaBeliBg.add(markupAmount);
			textFieldHargaJual.setText(AppUtil.formatStringIDR(hargaJualBg.intValue()));
			
			String valIsiKemasanStr = textFieldIsiKemasan.getText().trim();
			if(!valIsiKemasanStr.isEmpty()){
				String removeDotIsiKemasan = AppUtil.removeCharacters(valIsiKemasanStr, ".");
				
				if(!removeDotIsiKemasan.equalsIgnoreCase("0")){
					double hargaEceranDouble = hargaJualBg.doubleValue()/new Double(removeDotIsiKemasan);
					//System.out.println("hargaEceranDouble: "+hargaEceranDouble);
					BigDecimal hargaEceranBg = new BigDecimal(hargaEceranDouble).setScale(0, RoundingMode.HALF_UP);
					textFieldHargaJuaEceran.setText(AppUtil.formatStringIDR(hargaEceranBg.intValue()));
				}else{
					textFieldHargaJuaEceran.setText("0");
				}
			}
			
		/*} catch (Exception e) {
			System.out.println(e.getMessage());
		}*/
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if(source==btnCariBarang){
			AddProductToPurchaseDialog dialog = new AddProductToPurchaseDialog(this, this);
			dialog.setVisible(true);
			dialog.requestFocus();
			
			this.setVisible(false);
		}else if(source==btnSubmit){
			
			String productName = "";
			String productCode = "";
			if(productSelected!=null){
				productName = productSelected.getProductName();
				productCode = productSelected.getProductCode();
			}
			
			int tglInt = 1;
			int blnInt = 0;
			int thnInt = CalendarUtil.calendarNow().get(Calendar.YEAR);
			
			/*if(comboBoxTglExp.getSelectedItem()!=null){
				String tgl = (String) comboBoxTglExp.getSelectedItem();
				tglInt = Integer.parseInt(tgl);
			}*/
			
			if(comboBoxBlnExp.getSelectedItem()!=null){
				blnInt = comboBoxBlnExp.getSelectedIndex();
			}
			
			if(comboBoxThnExp.getSelectedItem()!=null){
				String thn = (String) comboBoxThnExp.getSelectedItem();
				thnInt = Integer.parseInt(thn);
			}
			
			Calendar calExp = CalendarUtil.calendarNow();
			calExp.set(Calendar.YEAR, thnInt);
			calExp.set(Calendar.MONTH, blnInt);
			calExp.set(Calendar.DAY_OF_MONTH, tglInt);
			
			String packUnit = "";
			if(comboBoxSatPak.getSelectedItem()!=null){
				packUnit = (String) comboBoxSatPak.getSelectedItem();
			}
			
			int quantityPack = 0;
			if(!textFieldJumlahPak.getText().trim().isEmpty()){
				String removeDot = AppUtil.removeCharacters(textFieldJumlahPak.getText().trim(), ".");
				quantityPack = Integer.parseInt(removeDot);
			}
			
			int quantityPerPack = 0;
			if(!textFieldIsiKemasan.getText().trim().isEmpty()){
				String removeDot = AppUtil.removeCharacters(textFieldIsiKemasan.getText().trim(), ".");
				quantityPerPack = Integer.parseInt(removeDot);
			}
			
			String quantityUnit = "";
			if(comboBoxSatKemasan.getSelectedItem()!=null){
				quantityUnit = (String) comboBoxSatKemasan.getSelectedItem();
			}
			
			BigDecimal hargaSatuanBg = BigDecimal.ZERO;
			BigDecimal priceUnitBg = BigDecimal.ZERO;
			if(!textFieldHargaSatuan.getText().trim().isEmpty()){
				String removeDot = AppUtil.removeCharacters(textFieldHargaSatuan.getText().trim(), ".");
				hargaSatuanBg = new BigDecimal(removeDot);
				double priceUnitDouble = hargaSatuanBg.doubleValue()/quantityPerPack;
				priceUnitBg = new BigDecimal(priceUnitDouble).setScale(0, RoundingMode.HALF_UP);
				
			}
			
			int ppnPersen = 0;
			BigDecimal ppnAmount = BigDecimal.ZERO;
			if(!textFieldPpnPersen.getText().trim().isEmpty()){
				String ppnPersenStr =  textFieldPpnPersen.getText().trim();
				ppnPersen = removePersen(ppnPersenStr);
				ppnAmount = AppUtil.amountPersentasi(ppnPersen, hargaSatuanBg, 0);
			}
			
			int markupPersen = 0;
			BigDecimal markupAmount = BigDecimal.ZERO;
			if(!textFieldMarkupPersen.getText().trim().isEmpty()){
				String markupPersenStr = textFieldMarkupPersen.getText().trim();
				markupPersen = removePersen(markupPersenStr);
				markupAmount = AppUtil.amountPersentasi(markupPersen, hargaSatuanBg, 0);
			}
			
			BigDecimal priceSellBg = BigDecimal.ZERO;
			if(!textFieldHargaJuaEceran.getText().trim().isEmpty()){
				String removeDot = AppUtil.removeCharacters(textFieldHargaJuaEceran.getText().trim(), ".");
				priceSellBg = new BigDecimal(removeDot);
			}
			
			
			PurchasesItem pi = new PurchasesItem();
			pi.setPurchase(purcahse);
			pi.setProduct(productSelected);
			pi.setProductName(productName);
			pi.setProductCode(productCode);
			pi.setExpireDate(calExp.getTime());
			pi.setPackUnit(packUnit);
			pi.setQuantityPack(quantityPack);
			pi.setQuantityPerPack(quantityPerPack);
			pi.setQuantityUnit(quantityUnit);
			pi.setPriceUnit(hargaSatuanBg);
			pi.setPpnPersen(ppnPersen);
			pi.setPpnAmount(ppnAmount);
			pi.setMarkupPersen(markupPersen);
			pi.setMarkupAmount(markupAmount);
			pi.setPriceBuy(priceUnitBg);
			pi.setPriceSell(priceSellBg);
			//pi.setPriceTotal(hargaSatuanBg.multiply(new BigDecimal(quantityPerPack).setScale(0, RoundingMode.HALF_UP)));
			
			PurchaseItemDao piDao = new PurchaseItemDao();
			piDao.saveOrUpdate(pi);
			
			//jika product ada, update, jika tidak insert new
			int availableStokBefore = productSelected.getAvailableStock();
			int availableStokUpdate = availableStokBefore + quantityPerPack;
			
			BigDecimal priceBuyBefore = productSelected.getPriceBuy();
			Date dateUpdatePrice = productSelected.getUpdatedPriceDate();
			if(!priceBuyBefore.equals(hargaSatuanBg)){
				dateUpdatePrice = CalendarUtil.dateNow();
			}
			
			Product p = productSelected;
			p.setExpireDate(calExp.getTime());
			p.setProductUnit(quantityUnit);
			p.setAvailableStock(availableStokUpdate);
			p.setPriceBuy(priceUnitBg);
			p.setPriceSell(priceSellBg);
			p.setUpdatedPriceDate(dateUpdatePrice);
			p.setUpdatedDate(CalendarUtil.dateNow());
			p.setUpdatedBy(user);
			
			ProductDao pDao = new ProductDao();
			pDao.saveOrUpdate(p); 
			
			HashMap<String, Object> hm = new HashMap<>();
			hm.put(PembelianInternalFrame.OBSERBER_FROM, PembelianInternalFrame.OBSERBER_PURCHASE_ITEM);
			//hm.put("purchaseItem", pi);
			observer.response(hm);
			
			this.setVisible(false);
		}else if(source==btnCancel){
			this.setVisible(false);
		}
	}

	@Override
	public void response(HashMap<String, Object> hm) {
		try {
			Product p = (Product) hm.get("product");
			if(p!=null){
				productSelected = p;
				textFieldNamaBarang.setText(p.getProductName());
				textFieldKodeBarang.setText(p.getProductCode());
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Exception: "+e.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
		}
		
	}

	@Override
	public void focusGained(FocusEvent e) {
		Object source = e.getSource();
		
		if(source==textFieldHargaSatuan){
			String valHargaSatuanStr = textFieldHargaSatuan.getText();
			if(valHargaSatuanStr.isEmpty()){
				valHargaSatuanStr = "0";
			}
			
			String removeDotHargaSatuan = AppUtil.removeCharacters(valHargaSatuanStr, ".");
			textFieldHargaSatuan.setText(removeDotHargaSatuan);
		
		}else if(source==textFieldPpnPersen){
			String valStr = textFieldPpnPersen.getText();
			if(valStr.isEmpty()){
				valStr = "0";
			}
			
			int persen = removePersen(valStr);
			textFieldPpnPersen.setText(Integer.toString(persen));
			
		}else if(source==textFieldMarkupPersen){
			String valStr = textFieldMarkupPersen.getText();
			if(valStr.isEmpty()){
				valStr = "0";
			}
			
			int persen = removePersen(valStr);
			textFieldMarkupPersen.setText(Integer.toString(persen));
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		Object source = e.getSource();
		
		if(source==textFieldIsiKemasan){
			updateTextFieldPrice();
			
		}else if(source==textFieldHargaSatuan){
			String valHargaSatuanStr = textFieldHargaSatuan.getText();
			if(valHargaSatuanStr.isEmpty()){
				valHargaSatuanStr = "0";
			}
			textFieldHargaSatuan.setText(AppUtil.formatStringIDR(Integer.parseInt(valHargaSatuanStr)));
			
			updateTextFieldPrice();
			
		}else if(source==textFieldPpnPersen){
			String valStr = textFieldPpnPersen.getText();
			if(valStr.isEmpty()){
				valStr = "0";
			}
			
			textFieldPpnPersen.setText(valStr+" %");
			
			updateTextFieldPrice();
			
		}else if(source==textFieldMarkupPersen){
			String valStr = textFieldMarkupPersen.getText();
			if(valStr.isEmpty()){
				valStr = "0";
			}
			
			textFieldMarkupPersen.setText(valStr+" %");
			
			updateTextFieldPrice();
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		Object source = e.getSource();
		
		if(source==textFieldHargaSatuan){
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				textFieldPpnPersen.requestFocus();
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
